

$(document).ready(function(e) {
    initTable();
});
/**
 * 初始化会员名录页面的两个会员表格
 */
function initTable() {
    var cTr = $("#cTr");
    var sTr = $("#sTr");

    var councilList = eval("("+$('#councilList').val()+")");
    var clength = councilList.length;

    var supervisorList = eval("("+$('#supervisorList').val()+")");
    var slength = supervisorList.length;

    for(var i=0;i< clength;i++){
        var index = clength-1-i;
        var num = clength-i;
        var trId = "ctrId" + index;
        var tr = '<tr id="'+trId+'">' +
            '<td>'+ num +'</td>' +
            '<td>'+ councilList[index].name +'</td>';
        if (councilList[index].link != null && councilList[index].link != ''){
            tr = tr + '<td><a href="'+ councilList[index].link +'" target="_blank" >'+ councilList[index].unit +'</a></td>';
        }else{
            tr = tr + '<td>'+ councilList[index].unit +'</td>';
        }
            tr = tr + '<td>'+ councilList[index].position +'</td></tr>';
        $(tr).insertAfter(cTr);
    }

    for(var i=0; i< slength; i++){
        var index = slength-1-i;
        var num = slength-i;
        var trId = "strId" + index;
        var tr = '<tr id="'+trId+'">' +
            '<td>'+ num +'</td>' +
            '<td>'+ supervisorList[index].name +'</td>';
        if (supervisorList[index].link != null && supervisorList[index].link != ''){
            tr = tr + '<td><a href="'+ supervisorList[index].link +'" target="_blank" >'+ supervisorList[index].unit +'</a></td>';
        }else{
            tr = tr + '<td>'+ supervisorList[index].unit +'</td>';
        }
            tr = tr +'<td>'+ supervisorList[index].position +'</td></tr>';
        $(tr).insertAfter(sTr);
    }
}

/**
 * "点击查看更多"
 */
function loadMore(type){

    var cTr = $("#cTr");
    var sTr = $("#sTr");

    // 需要加载数据的tr
    var mTr;
    // 后台的请求数据url
    var loadMoreUrl;
    // tr的id的字符部分
    var trname;

    // 理事
    if (type == 2) {
        loadMoreUrl = "/member/getCouncils";
        trname = "ctrId";
        mTr = cTr;
        removeTr(1);
        $("#more2").attr('style','display: none');
    } else if (type == 3) {
        loadMoreUrl = "/member/getSupervisors";
        trname = "strId";
        mTr = sTr;
        removeTr(0);
        $("#more1").attr('style','display: none');
    }

    $.ajax({
        url:loadMoreUrl,
        type:"get",
        dataType:"json",
        success:function(data){

            var comitteelList = eval(data);
            var clength = comitteelList.length;

            for(var i=0;i< clength;i++){
                var index = clength-1-i;
                var num = clength-i;
                var trId = trname + index;
                /*var tr = '<tr id="'+trId+'">' +
                    '<td>'+ num +'</td>' +
                    '<td>'+ comitteelList[index].name +'</td>' +
                    '<td>'+ comitteelList[index].unit +'</td>' +
                    '<td>'+ comitteelList[index].position +'</td>' +
                    '</tr>';*/
                var tr = '<tr id="'+trId+'">' +
                    '<td>'+ num +'</td>' +
                    '<td>'+ comitteelList[index].name +'</td>';
                if (comitteelList[index].link != null && comitteelList[index].link != ''){
                    tr = tr + '<td><a href="'+ comitteelList[index].link +'" target="_blank" >'+ comitteelList[index].unit +'</a></td>';
                }else{
                    tr = tr + '<td>'+ comitteelList[index].unit +'</td>';
                }
                tr = tr + '<td>'+ comitteelList[index].position +'</td></tr>';

                $(tr).insertAfter(mTr);
            }

        },
        error:function(XMLHttpRequest,textStatus, errorThrown) {
            // alert(errorThrown);
        }
    });
}

// “点击查看更多”前先清除表格数据
function removeTr(type) {
    var ctable = document.getElementById("ctable") ;
    //表格行数(表头两行和“点击查看更多”一行)
    var crows = ctable.rows.length;

    var stable = document.getElementById("stable") ;
    //表格行数(表头两行和“点击查看更多”一行)
    var srows = stable.rows.length;

    if (type == 1){
        for (var i=0;i<=crows-3;i++){
            var trId = "ctrId" + i;
            $('#'+trId).remove();
        }
    }else{
        for (var i=0;i<=srows-3;i++){
            var trId = "strId" + i;
            $('#'+trId).remove();
        }
    }
}